/** HomePanel */
package client.ui.panel;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.logging.Logger;

import javax.swing.JLabel;
import javax.swing.JPanel;

import client.ClientManager;
import client.service.AppointmentService;
import client.service.DoctorService;
import client.service.PatientService;
import domain.permissions.Permission;

/** Creates a home page containing links to other pages 
 * @author jessienewman
 * @version $Revision: 1.0 $
 */
public class HomePanel extends DefaultPanel {
	/**
	 * Field LOGGER.
	 */
	private static final Logger LOGGER = Logger.getLogger(ClientManager.class.getName());
	
	/**
	 * Field center.
	 */
	private JPanel center;
	
	/**
	 * Field c.
	 */
	private GridBagConstraints c;

	/** Creates a home page containing links to other pages */
	public HomePanel() {
		center = new JPanel();
		center.setBackground(Color.WHITE);
		this.add(center, BorderLayout.CENTER);
		center.setLayout(new GridBagLayout());
		c = new GridBagConstraints();
	} 
	
	/**
	 * Method addUser.
	 * @param userId Integer
	 */
	public void addUser(Integer userId) {
		center.removeAll();
		AppointmentPanel ap = new AppointmentPanel();
		c.gridy = 0;
		c.gridx = 0;
		if (ClientManager.SESSION.hasPermission(Permission.DOCTOR)) {
			center.add(new JLabel("Upcomming appointments:"), c);
			c.gridy++;
			ap.addAppointments(AppointmentService.getFutureDoctorAppointments(userId), 
					DoctorService.getDoctor(userId), true);
			if (AppointmentService.getFutureDoctorAppointments(userId).size() > 0) {
				center.add(ap.getCenterPanel(), c);
				ap.setNewAppointmentViewable(false);
			} else {
				center.add(new JLabel("(No upcomming appointments)"), c);
			}
		} else if (ClientManager.SESSION.hasPermission(Permission.PATIENT)) {
			center.add(new JLabel("Upcomming appointments:"), c);
			c.gridy++;
			ap.addAppointments(AppointmentService.getFuturePatientAppointments(userId), 
					PatientService.getPatient(userId), true);
			if (AppointmentService.getFuturePatientAppointments(userId).size() > 0) {
				center.add(ap.getCenterPanel(), c);
				ap.setNewAppointmentViewable(false);
			} else {
				center.add(new JLabel("(No upcoming appointments)"), c);
			}
		}
		LOGGER.info("updated homepanel");
	}
}
